<?php
class yangZhouService{

    
    //验证来源ip
    protected $_key             = 'airplane#@!';
    final function __construct()
    {
        $this->_return_rs = array(
			'state'        => FALSE,
			'data'         => array(),
            'description'  => ''
         );
    }

	/**
	*获取数据库链接
	*/
	private function db_connect(){
		$use_dbver='5.0';
		$db_char='utf8';
		$db_dbname='log';
		$link=@mysql_connect('db-log-ro.cdn35.com:33064','redis','feeyo');
		if(!$link){
			echo"Cann't connect to DB!";
			exit();
		}
		//编码
		if($use_dbver>='4.1'){
			$q='';
			if($db_char){
				$q='character_set_connection='.$db_char.',character_set_results='.$db_char.',character_set_client=binary';
			}
			if($use_dbver>='5.0'){
				$q.=(empty($q)?'':',').'sql_mode=\'\'';
			}
			if($q){
				@mysql_query('SET '.$q);
			}
		}
		@mysql_select_db($db_dbname);
		mysql_query("set wait_timeout=2880000") or die(mysql_error());
		mysql_query("set interactive_timeout=24*3600*30") or die(mysql_error());
		return $link;
	}


	final public function getAirplaneData($pass)
    {
    	// 校验合法性
    	$this->checkPass($pass);
    	if (!empty($this->_return_rs['rs_description'])){
    		return $this->_return_rs;
    	}
    	
		$sql="select
		 result.id
		,result.FlightNum3
		,result.FlightNum2
		,result.Registration
		,result.DepCode
		,ArrCode
		,result.Lat
		,result.Lng
		,result.Speed
		,result.Altitude
		from
		(
		select
		 a.id
		,a.FlightNum3
		,a.FlightNum2
		,a.Registration
		,a.DepCode,ArrCode
		,a.Lat
		,a.Lng
		,a.Speed
		,a.Altitude
		,@v_rownum := @v_rownum+1
		,if(@V_Registration=a.Registration,@v_rank:=@v_rank+1,@v_rank:=1) as 'rank_num'
		,@V_Registration:=a.Registration
		from
		(
		select
		id,
		FlightNum3,
		FlightNum2,
		Registration,
		DepCode,ArrCode,
		Lat,
		Lng,
		Speed,
		Altitude
		from `fa_airnav_".date('Ymd')."`
		where DataSource='Veryzhun_YTY' and `Lat`<>0 and `Lng`<>0 and Registration <>''
		order by Registration, id desc
		)a,
		(
		select @v_rank:=0,@v_rownum:=0,@V_Registration:=null
		) b
		) result
		where result.rank_num=1";
		$link = $this->db_connect();
		$result = mysql_query ($sql, $link);

		$result_array = array();
		while(($tmp = mysql_fetch_assoc($result)) !== false) {
			$temp = array();
			$temp['flight'] = $tmp['Registration'];
			$temp['x'] = $tmp['Lng'];
			$temp['y'] = $tmp['Lat'];
			$temp['speed'] = $tmp['Speed'];
			$temp['height'] = $tmp['Altitude'];
			$result_array[$tmp['Registration']] = $temp;
		}


		// 给js准备数据
		$result_array = json_encode($result_array);
        
        $this->_return_rs = array(
			'state'        => TRUE,
			'data'         => $result_array,
            'description'  => '成功获取数据'
        );
        
        return $this->_return_rs;
    }

	/**
     * 必须存在，webserice 自动调用些方法
     * @return null
     */
    final public function index()
    {
        return null;
    }

	final public function checkPass($fpass='')
    {   
        //密钥为空验证
        if($fpass == '' || !is_string($fpass)){            
            $this->_return_rs['rs_description'] = '密钥不能为空！';
            return $this->_return_rs;
        }
        
        //密钥完匹配验证
        if(strcmp($fpass, $this->_key) != 0){
        	$this->_return_rs['rs_description']='对不起，您的密钥错误！';
        	return $this->_return_rs;
        }
	}

	/**
     * 魔术函数
     * @param type $name
     * @param type $arguments
     * @return type
     */
    final public function __call($name, $arguments) {
        //返回
        $this->_return_rs['rs_description']="调用的方法[<font color='red'>{$name}</font>]不存在！";
        return $this->_return_rs;
    }
}



$data = @$HTTP_RAW_POST_DATA;
$data = file_get_contents('php://input');
$server = new SoapServer(null, array('uri'=>'yangZhouService', 'encoding'=>'utf8'));
$server->setClass('yangZhouService');
$server->handle($data);
?>